use std::fmt::{Display, Formatter, Result};
use crate::main;

#[derive(Debug)]
pub enum Charset {
    UTF8,
    GBK,
    GB2312,
    ASCII,
    UNICODE,
}

/// desc  : 展示 Trait
/// author: Graves
/// date  : 2022/7/8 13:03
impl Display for Charset {
    /// desc  : impl
    /// author: Graves
    /// date  : 2022/7/8 13:04
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        match self {
            Charset::UTF8 => write!(f, "unicode"),
            Charset::GBK => write!(f, "gbk"),
            Charset::GB2312 => write!(f, "gb2312"),
            Charset::ASCII => write!(f, "ascii"),
            Charset::UNICODE => write!(f, "unicode"),
        }
    }
}

/// desc  : impl
/// author: Graves
/// date  : 2022/7/8 13:09
impl From<String> for Charset {
    /// desc  : match str to enum, default panic!
    /// author: Graves
    /// date  : 2022/7/8 13:13
    fn from(str: String) -> Self {
        match str {
            str if str.eq("utf8") => Charset::UTF8,
            str if str.eq("gbk") => Charset::GBK,
            str if str.eq("gb2312") => Charset::GB2312,
            str if str.eq("ascii") => Charset::ASCII,
            str if str.eq("unicode") => Charset::UNICODE,
            _ => panic!("服务字符集编码设置失败, 请选择有效的字符集编码进行配置[utf8/gbk/unicode/gb2312/ascii]!", )
        }
    }
}

/// desc  : 校验 Trait
/// author: Graves
/// date  : 2022/7/8 12:52
pub trait Valid {
    /// desc  : 字符集配置校验
    /// author: Graves
    /// date  : 2022/7/8 12:52
    fn valid_charset(c: String);
}

/// desc  : Trait 实现
/// author: Graves
/// date  : 2022/7/8 12:52
impl Valid for Charset {
    /// desc  : 字符集配置校验
    /// author: Graves
    /// date  : 2022/7/8 12:52
    fn valid_charset(c: String) {
        println!("服务字符集配置 : {:?}", Charset::from(c));
    }
}